package feeinterface.protocol.reqn;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.ulwx.tool.CTime;
import com.ulwx.tool.HttpUtils;
import com.ulwx.tool.NetUtils;
import com.ulwx.tool.StringUtils;
import com.ulwx.type.TString;

import feeinterface.dao.BaseImsiPhoneDao;
import feeinterface.dao.ChannelDao;
import feeinterface.dao.ChannelPhoneSendLogDao;
import feeinterface.modeldb.BaseImsiPhone;
import feeinterface.modeldb.Channel;
import feeinterface.modeldb.ChannelPhoneSendLog;
import feeinterface.protocol.req.ProExchange;
import feeinterface.protocol.resn.M201003;
import feeinterface.utils.PhoneUtils;

public class T201003 extends ProExchange {

	private static Logger log = Logger.getLogger(T201003.class);

	public String Protocol;// string(6) 协议编号:：201003
	public Integer Ver;// int 扣费模块版本号，，从0开始
	public Integer PacketID;// int 打包id。
	public Integer VmVer; // int 版本ID，两位，从10开始
	public Integer PlatformID;// int 见5平台id 3000
	public String Mobile;// String（22） 手机号码
	public String IMSI; // string(15) 手机卡串号
	public String SmsCenter;// string 短信中心号码
	public String FeeID;// string 扣费id
	public String Log; // string 日志格式，utf-8 url转义
	// <ChannelID>||<sendcnt>||<SendTime>||<Status>@@<ChannelID>||<sendcnt>||<SendTime>||<Status>@@...
	public Integer Test;// int 0：真机 1：pc模拟器

	@Override
	public Object genBean() throws Exception {
		// TODO Auto-generated method stub

		String logStr = StringUtils.trim(this.Log);
		String[] strs = logStr.split("@@");
		this.IMSI = StringUtils.trim(this.IMSI);

		TString carrier=new TString();
	    this.getAreaID("", IMSI, this.SmsCenter, carrier);
		log.debug("log="+this.Log);
		for (int i = 0; i < strs.length; i++) {
			String[] temps = strs[i].split("\\|\\|");
			if (temps.length <4)
				continue;
			Integer chid = Integer.valueOf(temps[0]);
			String SendTime = temps[2];
			Integer Status = Integer.valueOf(temps[3]);
			Integer type =Integer.valueOf(temps[1]);
			ChannelPhoneSendLog cps = new ChannelPhoneSendLog();
			cps.setChannelID(chid);
			cps.setCOID(PacketID);
			cps.setFeeID(this.FeeID);
			cps.setIMSI(this.IMSI);
			cps.setMobile(this.Mobile);
			cps.setSendCnt(1);
			cps.setSendTime(SendTime);
			cps.setSmsCenter(this.SmsCenter);
			cps.setStatus(Status);
			cps.setVer(this.Ver);
			cps.setVmVer(this.VmVer);
			cps.setCreatime(CTime.getCurrentDateTime());
			//ChannelPhoneSendLog 要 加上地区和城市

			ChannelPhoneSendLogDao.insert(cps);

			//Channel ch = ChannelDao.getBy(chid);
			if (type == 2) {// wap
				Map<String, String> map = new HashMap<String, String>();
				map.put("Mobile", this.Mobile);
				map.put("IMSI", this.IMSI);
				map.put("COID", this.PacketID + "");
				map.put("ChannelID", chid + "");
				map.put("FeeID", this.FeeID);

				if (Status == 1) {
					Status = 0;
				} else if (Status == 0) {
					Status = 1;
				}
				map.put("Status", Status + "");
				HttpServletRequest hr=(HttpServletRequest)this.getRequest().getObject("HttpRequest");
				int port=hr.getServerPort();
				
				String url = "http://localhost:" +
						port +
						"/feechannel/mo/wap";
				url = NetUtils.getMethodUrl(url, map, "utf-8");
				try {
					byte[] bs = HttpUtils.getBytes(url, 5000);
				} catch (Exception e) {
					log.error("", e);
				}
	
			}
		}
		M201003 m = new M201003();
		m.Message = "成功";
		BaseImsiPhone bi = BaseImsiPhoneDao.getByIMSI(this.IMSI);
		if (bi != null) {
			m.Mobile = bi.getPhone();
		} else {
			m.Mobile = "";
		}
		m.Status = 1;

		// HttpUtils.get
		return m;
	}

	@Override
	public String validate() throws Exception {
		// TODO Auto-generated method stub
		SmsCenter = PhoneUtils.getAdjustedPhone(this.SmsCenter);
		return null;
	}
	public static void main(String[] args) {
		System.out.println("10013||4||2012.3.26.15.7.200@@".split("@@").length);
	}

}
